rm(list=ls(all=TRUE))
library(foreign)
library(metafor)
library(clubSandwich)
library(dplyr)

setwd("~/Dropbox/Apps/ShareLaTeX/Minimal Effects/replication/data")

data <- read.csv("master_sheet_output.csv", stringsAsFactors = FALSE)

data <- subset(data, !is.na(Candidate.Effect.with.Covars))
data$ExperimentName <- gsub("Canvass", "Experiment", data$ExperimentName)
data$label <- paste0(data$ExperimentName, ' - ', data$Seat, ' - ', data$Treatment.Mode)

# Overall Table 
data <- data[order(data$Date.Canvass),]
data$label.for.table <- with(data, paste0(
  label, ' & ', Date.Canvass, ' & ',
  round(Candidate.Effect.with.Covars, 3), ' (',
  round(Candidate.Effect.SE.with.Covars, 3), ') \\\\'))
data$label.for.table <- gsub("Original Study - ", "", data$label.for.table)
data$label.for.table <- gsub(" - Canvass", "", data$label.for.table)
data$label.for.table <- gsub(" - Warm Lit Drop", "", data$label.for.table)
write.csv(data$label.for.table, 'label_for_table.csv')

data$Candidate.Effect.V.with.Covars <- data$Candidate.Effect.SE.with.Covars^2

#Get effects for table
#Canvasses During 2015 Primary and Special Election
elect.2015 <-  subset(data, data$ExperimentName %in% c("Original Study - PA Experiment 2015",
                                                       "Original Study - PA Experiment 2015 Follow-Up",
                                                       "Original Study - WA Experiment 2015",
                                                       "Original Study - WA Experiment 2015 Post-Election"))
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
       data = elect.2015)
meta

#Literature Drop and Minimal Canvass Interaction
lit.drop <-  subset(data, data$label %in% c("Original Study - NC Experiment - Governor - Warm Lit Drop",
                                                      "Original Study - NC Experiment - Supreme Court - Warm Lit Drop",
                                                      "Original Study - NC DID - Governor - Warm Lit Drop",
                                                      "Original Study - NC DID - Supreme Court - Warm Lit Drop"))
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
               data = lit.drop)
meta

# Remove non-partisan and non-general races
data <- subset(data, !(data$label %in% c("Original Study - NC DID - Supreme Court - Warm Lit Drop",
                                         "Original Study - NC Experiment - Supreme Court - Warm Lit Drop",
                                         "Original Study - PA Experiment 2015 - Mayor - Canvass",
                                         "Original Study - PA Experiment 2015 Follow-Up - Mayor - Canvass",
                                         "Original Study - WA Experiment 2015 Post-Election - State Legislator - Canvass",
                                         "Original Study - WA Experiment 2015 - State Legislator - Canvass")))

#Canvasses $>$2 Months Before Election
early.data <- subset(data, data$ExperimentName %in% c("Original Study - OH Experiment May",
                                                      "Original Study - OH Experiment August"))
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
       data = early.data)
meta

# Canvasses Within 2 Months Of Election - DIDs
late.data <- subset(data, !(data$ExperimentName %in% c("Original Study - OH Experiment May",
                                                       "Original Study - OH Experiment August",
                                                       "Original Study - OH Experiment Election Day")) &
                      grepl('DID', data$ExperimentName) & data$Treatment.Mode == 'Canvass')
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
       data = late.data)
meta

# Canvasses Within 2 Months Of Election - experiments
late.data <- subset(data, !(data$ExperimentName %in% c("Original Study - OH Experiment May",
                                                       "Original Study - OH Experiment August")) &
                      !grepl('DID', data$ExperimentName) & data$Treatment.Mode == 'Canvass')
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
               data = late.data)
meta


# Convert to pp and calcualte variance
data$Candidate.Effect.with.Covars <- data$Candidate.Effect.with.Covars * 50
data$Candidate.Effect.SE.with.Covars <- data$Candidate.Effect.SE.with.Covars * 50
data$Candidate.Effect.V.with.Covars <- data$Candidate.Effect.SE.with.Covars^2

data$PID.Interaction.Covars <- data$PID.Interaction.Covars * 50
data$PID.Interaction.SE.Covars <- data$PID.Interaction.SE.Covars * 50
data$PID.Interaction.V.Covars <- data$PID.Interaction.SE.Covars^2

data$AfAm.Interaction.Covars <- data$AfAm.Interaction.Covars * 50
data$AfAm.Interaction.SE.Covars <- data$AfAm.Interaction.SE.Covars * 50
data$AfAm.Interaction.V.Covars <- data$AfAm.Interaction.SE.Covars^2

data <- subset(data, !(data$ExperimentName %in% c("Original Study - OH Canvass Election Day")))


# Remove non-canvass studies
#Remove NC Court and Gov --> Not canvass
data <- subset(data, !(data$label %in% c("Original Study - NC DID - Governor - Warm Lit Drop",
                                         "Original Study - NC Experiment - Governor - Warm Lit Drop")))


## OVERALL EFFECTS
# Overall
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
               data = data)
coef_test(meta, vcov = "CR2")

# Just not close to election
not.close.to.election <- subset(data,
                            Data == 'OH_EarlyCanvass_2016.dta' | # Canvass in May
                              Data == 'OH_LateCanvass_2016.dta') # Canvass Aug 25
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
               data = not.close.to.election)
coef_test(meta, vcov = "CR2")

# Just close to election
close.to.election <- subset(data,
                            Data != 'OH_EarlyCanvass_2016.dta' & # Canvass in May
                              Data != 'OH_LateCanvass_2016.dta') # Canvass Aug 25
meta <- rma.mv(yi = Candidate.Effect.with.Covars, V = Candidate.Effect.V.with.Covars, random = list(~1 | Experiment),
               data = close.to.election)
coef_test(meta, vcov = "CR2")

pdf('../figures/forest_wa_late.pdf', width = 10, height = 6)
forest(meta, slab = close.to.election$label, cex = .8,
       alim=c(-40,40), xlim=c(-85,65),
       xlab = 'Estimated Treatment Effect (CACE) in Percentage Points and 95% Confidence Interval \n Subset: Working America Canvass Studies - Treatment Within 2 Months of Election Day')
dev.off()


# PID Interaction
meta <- rma.mv(yi = PID.Interaction.Covars, V = PID.Interaction.V.Covars, random = list(~1 | Experiment),
               data = data)
coef_test(meta, vcov = "CR2")

pdf('../figures/forest_wa_late_pidinteraction.pdf', width = 10, height = 6)
forest(meta, slab = data$label, cex = .8, xlab = "Interaction Effect with Partisanship",
       alim=c(-40,40), xlim=c(-85,65))
dev.off()


# Afam Interaction
meta <- rma.mv(yi = AfAm.Interaction.Covars, V = AfAm.Interaction.V.Covars, random = list(~1 | Experiment),
               data = data)
coef_test(meta, vcov = "CR2")

pdf('../figures/forest_wa_late_afaminteraction.pdf', width = 10, height = 6)
forest(meta, slab = data$label, cex = .6, xlab = "Interaction Effect with African American",
       alim=c(-40,40), xlim=c(-85,65))
dev.off()